package com.example.huangliting.experimentalarrangementsystem.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.huangliting.experimentalarrangementsystem.R;
import com.example.huangliting.experimentalarrangementsystem.util.DateUitls;
import com.example.huangliting.experimentalarrangementsystem.util.HttpUtils;
import com.example.huangliting.experimentalarrangementsystem.util.Utils;

import org.json.JSONObject;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ClassDetailActivity extends AppCompatActivity {

    private final String getClassDetailUrl = HttpUtils.headerUrl+"timetable/getClassDetail";
    private TextView className_tv,teacherName_tv,classRoom_tv,classTime_tv,batchNum_tv;
    private Button backBtn;
    private int cid;

    private JSONObject dataJSON = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.class_detail);
        initView();
    }

    private void initView() {
        className_tv = findViewById(R.id.classname_detail_tv);
        teacherName_tv = findViewById(R.id.tname_detail_tv);
        classRoom_tv = findViewById(R.id.classroom_detail_tv);
        classTime_tv = findViewById(R.id.classtime_detail_tv);
        batchNum_tv = findViewById(R.id.batchnum_detail_tv);

        Intent intent = getIntent();
        cid = intent.getIntExtra("cid",0);

        Map<String, String> params = new HashMap<String, String>();
        params.put("cid", String.valueOf(cid));
        getDataByCid(params, "utf-8");


        /*返回操作*/
        backBtn = findViewById(R.id.toolbar_left_btn);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                finish();
            }
        });
    }


    private final String[] items = {"实验","考试","培训","比赛","其他"};
    //获取当周我的课程Json和解析
    Handler myClassHandler = new Handler() {

        @SuppressLint("WrongConstant")
        public void handleMessage(android.os.Message msg) {
            try {
                JSONObject jsonObj = new JSONObject((String) msg.obj);
                dataJSON = jsonObj.getJSONObject("timetable");
                className_tv.setText(dataJSON.getString("lessonName"));
                //aNameIG.setText(dataJSON.getString("applicationName"));
                classRoom_tv.setText(dataJSON.getString("lname"));
                /*日期显示*/
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String classDateStr = dataJSON.getString("classDate");
                Date classdt = null;
                try {
                    classdt = format.parse(classDateStr);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                Calendar cal = Calendar.getInstance();
                cal.setTime(classdt);
                classTime_tv.setText(classDateStr+" "+ DateUitls.formateDay(cal.get(Calendar.DAY_OF_WEEK))+"   "+dataJSON.getInt("classNum")+"-"+(dataJSON.getInt("classNum")+dataJSON.getInt("classLength")-1)+"节");
                teacherName_tv.setText(dataJSON.getString("tname"));
                batchNum_tv.setText("第"+dataJSON.getString("batchNum")+"批"+"  ("+items[dataJSON.getInt("classClassify")]+")");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    //当周我的课程请求
    private void getDataByCid(final Map<String, String> params, final String encode) {
        Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    URL url = new URL(getClassDetailUrl);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setConnectTimeout(5000);
                    conn.setReadTimeout(5000);

                    //拼接出要提交的数据的字符串
                    byte[] data = HttpUtils.getRequestData(params, encode).toString().getBytes();

                    //添加post请求的两行属性
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    conn.setRequestProperty("Content-Length", String.valueOf(data.length));

                    //设置打开输出流
                    conn.setDoOutput(true);
                    //拿到输出流
                    OutputStream os = conn.getOutputStream();
                    //使用输出流往服务器提交数据
                    os.write(data);
                    if (conn.getResponseCode() == 200) {
                        InputStream is = conn.getInputStream();
                        String text = Utils.getTextFromStream(is);
                        Message msg = myClassHandler.obtainMessage();
                        msg.obj = text;
                        myClassHandler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        t.start();
    }


}
